package com.isczy.oss.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.isczy.oss.service.OssService;
import com.isczy.oss.utils.YamlConstant;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/********************************************
 * @Brief: oss上传
 * @Version: 1.0
 * @Date: 2020年12月30日 10:18:18
 * @Author: ChangZiYang
 * ******************************************
 */
@Service
public class OssServiceImpl implements OssService {

    /***
     * ***************************************
     * 功能描述:上传文件到oss
     * @param file
     * @return {@link String}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2020-12-30 10:28】
     * @author modify:
     */
    @Override
    public String uploadOssFile(MultipartFile file) {

        String endpoint = YamlConstant.ENDPOINT;
        String accessKeyId = YamlConstant.ACCESS_KEY_ID;
        String accessKeySecret = YamlConstant.ACCESS_KEY_SECRET;
        String bucketName = YamlConstant.BUCKET_NAME;
        OSS ossClient = null;
        InputStream inputStream = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 上传文件
            inputStream = file.getInputStream();

            String fileName = DateUtil.format(new Date(),"yyyy/MM/dd")+"/"
                    +IdUtil.getSnowflake(1,1).nextIdStr()
                    +file.getOriginalFilename();
            ossClient.putObject(bucketName, fileName, inputStream);
            //获取文件上传后的路径
            //https://czy-edu.oss-cn-hangzhou.aliyuncs.com/3_weixin_45240169_1608080128.jpg
            return "https://"+bucketName+"."+endpoint+"/"+fileName;
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("创建OSSClient实例,上传文件到oss失败")
                    .build();
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw MyException.builder()
                            .exception(e)
                            .code(ResultCode.ERROR)
                            .className(getClass().getName())
                            .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                            .message("inputStream close failed!")
                            .build();
                }
            }
            if (ossClient != null){
                ossClient.shutdown();
            }
        }
    }
}
